import moment from "moment";
import * as ddscode from "dingtalk-jsapi";
let app = getApp();

Page({
  data: {
    currentContentId: 0,   //默认展示第一个未完成的内容
    purchaseOrderList: [],
    purchaseList: [],

    purchaseListCp:[],
    purchaseListSh:[],
    purchaseListCg:[],

    slotModalShow: false,
    slotButtonList: [
      {
        name: '关闭',
      }
    ],

    StatusOptions: [{
      value: 0,
      name: '交通'
    },
    {
      value: 1,
      name: '餐饮'
    }],
    statusindex: '',
    startData: '',  //时间相关
    endData: '',

    activeIndex: 0,
    // checked: true,

    page:1,
    page_size:20,
    searchInput:"",   //用于保存搜索框的值
    dowm:true,
    showEndTip: false, // 控制是否显示到底提示
  },

  onLoad() {
    var that = this;
    var date = new Date();
    that.setData({
      purchaseOrderList: [],
    })
    setTimeout(() => {
      this.setData({
        activeIndexDefault: 1
      })
    }, 2000)
    
    this.getPurchaseList('');   
    this.getPurchaseCgList('')

    // 括号里面传个但因后这是因为在页面加载时就获取配货单未完成数据，而不进行任何搜索。
    // 在 getPurchaseOrderList 函数中，如果 searchTerm 参数为空字符串，
    // 则可能会获取所有配货单数据，或者根据您的业务逻辑来处理。
    // 因此，通过传入空字符串 ''，您可以在页面加载时初始化数据或获取默认数据。
  },

  onShow() {
    var that = this;
    // that.setData({
    //   purchaseOrderList: [],
    // })
    // that.getPurchaseOrderList()
  },


  // 扫码
  onSearchShop() {
    const that = this;
    dd.scan({
        type: 'qr',
        success: (res) => {
            console.log("扫码结果", res.code);
            that.onSearchInput({ detail: { value: res.code } }); // 调用输入框值改变事件，并传入扫码结果
        },
        fail: (err) => {
            // 扫码失败后的回调函数，可以根据实际需求进行处理
        }
    });
},


// 点击了哪个状态
  toApprovalHall(e) {
    var that = this
    const contentId = e.currentTarget.dataset.id; // 获取点击的内容的id
    console.log("我点击了第几个=====>",contentId);
    var that = this;
    that.setData({
      currentContentId: contentId, // 更新当前展示的内容id
      page:1,
      purchaseOrderList:[],
      dowm:true,
    });

    if(contentId < 1){
      this.getPurchaseList('');
      this.getPurchaseCgList('')

    }else if (contentId > 0 && contentId < 2) {
      this.getPurchaseLists('');
      this.getPurchaseCgList('')

    }else if (contentId >=2 ) {
      this.getPurchaseOrderList(''); // 假设这里填写了另一个条件
    }

  },

  // 点击查看详情跳转
  goprojectFollow(e){
    var item = e.currentTarget.dataset.item; 
    console.log("item==>",item)
    item.报销截图 = item.报销截图.replace(/\&/g, "|");
    dd.navigateTo({
      url: '/pages/projectFollow/projectFollow?item=' + JSON.stringify(item) +"&id=2",
    });
  },

  //状态下拉框 状态改变可以拿到下标 赋值 还可以根据下标知道选中的是哪个值 
  statusChange(e) {
    this.setData({
      statusindex: e.detail.value,
      status: e.detail.value
    })
  },

  // 获取输入内容进行搜索
  onSearchInput(e){
    console.log("e===>>",e)
    var that = this;
    that.setData({
      searchInput:e,
      page:1,
      dowm:true,
      purchaseOrderList:[]
    })
    
    that.getPurchaseOrderList();

  },

// 用于监听输入框值改变事件
onSearchInput(e) {
  var that = this;
  var searchTerm = e.detail.value.trim(); // 获取输入框的搜索内容并去除首尾空格
  console.log("输入框用来搜索的内容", searchTerm);

  // 清除之前的搜索定时器
  clearTimeout(that.data.searchTimer);

  // 设置一个新的定时器，在用户停止输入300毫秒后执行搜索操作
  var searchTimer = setTimeout(function() {
      that.setData({
          searchTimer: null // 清空定时器
      });


      if (that.contentId < 1) {
        that.getPurchaseList(searchTerm);
      } else if (that.contentId = 1) {
          that.getPurchaseLists(searchTerm);
      } else {
          that.getPurchaseOrderList(searchTerm);
      }

      // that.getPurchaseOrderList(searchTerm); // 执行搜索操作
      // that.getPurchaseList(searchTerm); // 执行搜索操作
      // that.getPurchaseLists(searchTerm)
  }, 300);

  // 将定时器存储在数据中
  that.setData({
      searchTimer: searchTimer
  });
},

// 清除搜索框内容
onSearchClear(e) {
  var that = this;
  that.setData({
    searchInput: '', // 清空搜索框内容
    page: 1,
    dowm: true,
    purchaseOrderList: []
  });
},


  // 获取采购分解单
  getPurchaseCgList(searchTerm) {
    console.log("进入了");
    var that = this;

    // 在请求开始前先清空数组
    that.setData({
      purchaseList: []  // 清空数组
    });

    var currentContentId = that.data.currentContentId
    console.log("ddddddddddd",);
    var bx_status = 0;
    if(currentContentId == 0){
      bx_status = 0
    }else if(currentContentId == 1){
      bx_status =1
    }

    var shujuKu = app.globaldata.cipher_code
    console.log("数据库==>",shujuKu);

    var data = JSON.stringify({
      "q": {
        "encrypted_text": "jU6VvGdH4KVhu34y9VEKRg==NMp1rGAtbSB7dQPhhqqVeMOQXxSSLPu/5NgBqqqlFxE=",
        "key": "65af0c184f878a00",
        "sql": "select T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNAME as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID, t4.fname AS 销售员,count(t2.F_ZYKJ_Base3) AS 总款数,sum(t2.F_ZYKJ_BaseQty1 )as 总数量,sum(t2.F_ZYKJ_BaseQty1-t2.F_ZYKJ_Qty_83g) as 未拣数量,t2.F_ZYKJ_CHECKBOX11 as 是否包装 from ZYKJ_SCRWD T1 left JOIN F_SCRWD_Entry1 T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER_L T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C' and t1.FBILLNO LIKE '%" + searchTerm + "%' and t1.FBillTypeId = '6628a101c794d4' and t2.F_ZYKJ_CHECKBOX11 = '" + bx_status + "' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNAME ,t1.F_ZYKJ_Base_tzk1,t4.fname,t2.F_ZYKJ_CHECKBOX11 ORDER BY t1.FID",
        "fields": [
          "FID","订单号","单据编码","业务日期","客户ID","客户名称","销售员ID","销售员","总款数","总数量","未拣数量","是否包装"
        ]
      },
      "p": {
        "page_number": 1,
        "page_size": 20
      }
      })

      console.log("获取采购分解单的data",data);

      dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
        console.log('获取采购分解单数据响应200000==>', res.data)
          var list = res.data.data;

          if(list.length < that.data.page_size){
            that.setData({
              dowm:false
            })
          }

          that.setData({
            purchaseListCg:list
          })
          console.log("采购分解单数据",that.data.purchaseListCg);
        } else {
          console.log("采购分解单无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },

  // 获取拣货单成品拣货
  getPurchaseList(searchTerm) {
    console.log("进入了");
    var that = this;

    // 在请求开始前先清空数组
    that.setData({
      purchaseList: []  // 清空数组
    });

    var currentContentId = that.data.currentContentId
    console.log("ddddddddddd",);
    var bx_status = 0;
    if(currentContentId == 0){
      bx_status = 0
    }else if(currentContentId == 1){
      bx_status =1
    }

    var shujuKu = app.globaldata.cipher_code
    console.log("数据库==>",shujuKu);

    var data = JSON.stringify({
        "q": {
          "encrypted_text": shujuKu,
          "key": "65af0c184f878a00",
          "sql":"select T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNAME as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID, t4.fname AS 销售员,count(t2.F_ZYKJ_Base3) AS 总款数,sum(t2.F_ZYKJ_BaseQty1 )as 总数量,sum(t2.F_ZYKJ_BaseQty1-t2.F_ZYKJ_Qty_83g) as 未拣数量,t2.F_ZYKJ_CHECKBOX11 as 是否包装 from ZYKJ_SCRWD T1 left JOIN F_SCRWD_Entry1 T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER_L T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C' and t1.FBILLNO LIKE '%" + searchTerm + "%' and t1.FBillTypeId = '662f0114a2b069' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNAME ,t1.F_ZYKJ_Base_tzk1,t4.fname,t2.F_ZYKJ_CHECKBOX11 ORDER BY t1.FID",
          "fields": [
            "FID","订单号","单据编码","业务日期","客户ID","客户名称","销售员ID","销售员","总款数","总数量","未拣数量","是否包装"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
      })

      console.log("获取是否装包的data",data);

      dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        console.log('获取拣货单未完成数据响应==>', res.data)
        if (res.data.code == 200) {
        console.log('获取拣货单未完成数据响应200000==>', res.data)
          var list = res.data.data;
        
          if(list.length < that.data.page_size){
            that.setData({
              dowm:false
            })
          }

          that.setData({
            purchaseListCp:list
          })
          console.log("成品拣货单数据",that.data.purchaseListCp);

          // 假设 that.data.purchaseListCg 是你的 purchaseListCg 数组数据
          let purchaseListCg = that.data.purchaseListCg;

          // 将 purchaseListCg 数组中的数据添加到 purchaseListCp 数组中
          let combinedList = that.data.purchaseListCp.concat(purchaseListCg);


          // 更新数据
          that.setData({
            purchaseListCp: combinedList
          });

          console.log("zzzzzzzzzzzzzzzzzzzzzzz",that.data.purchaseListCp);

        } else {
          console.log("配货未完成的无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },

  // 获取拣货单散货拣货单
  getPurchaseLists(searchTerm) {
    console.log("进入了");
    var that = this;

    // 在请求开始前先清空数组
    that.setData({
      purchaseList: []  // 清空数组
    });

    var currentContentId = that.data.currentContentId
    console.log("ddddddddddd",);
    var bx_status = 0;
    if(currentContentId == 0){
      bx_status = 0
    }else if(currentContentId == 1){
      bx_status =1
    }

    var shujuKu = app.globaldata.cipher_code
    console.log("数据库==>",shujuKu);

    var data = JSON.stringify({
        "q": {
          "encrypted_text": shujuKu,
          "key": "65af0c184f878a00",
          "sql":"select T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t3.FNAME as 客户名称,t1.F_ZYKJ_Base_tzk1 as 销售员ID, t4.fname AS 销售员,count(t2.F_ZYKJ_Base3) AS 总款数,sum(t2.F_ZYKJ_BaseQty1 )as 总数量,sum(t2.F_ZYKJ_BaseQty1-t2.F_ZYKJ_Qty_83g) as 未拣数量,t2.F_ZYKJ_CHECKBOX11 as 是否包装 from ZYKJ_SCRWD T1 left JOIN F_SCRWD_Entry1 T2 ON T2.FID =T1.FID LEFT JOIN T_BD_CUSTOMER_L T3 ON T3.FCUSTID =T1.F_ZYKJ_Base_apv left join T_BD_OPERATORENTRY_L t4 on t4.FENTRYID=t1.F_ZYKJ_Base_tzk1 where t1.FDocumentStatus ='C' and t1.FBILLNO LIKE '%" + searchTerm + "%' and t1.FBillTypeId = '662f0125a2b0f7' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t3.FNAME ,t1.F_ZYKJ_Base_tzk1,t4.fname,t2.F_ZYKJ_CHECKBOX11 ORDER BY t1.FID",
          "fields": [
            "FID","订单号","单据编码","业务日期","客户ID","客户名称","销售员ID","销售员","总款数","总数量","未拣数量","是否包装"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
      })

      console.log("获取是否装包的data",data);

      dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
        console.log('获取拣货单未完成数据响应200000==>', res.data)
          var list = res.data.data;
        
          that.setData({
            purchaseListSh:list
          })
          console.log("散货拣货单数据",that.data.purchaseListSh);

          // 假设 that.data.purchaseListCg 是你的 purchaseListCg 数组数据
          let purchaseListCg = that.data.purchaseListCg;

          // 将 purchaseListCg 数组中的数据添加到 purchaseListSh 数组中
          let combinedList = that.data.purchaseListSh.concat(purchaseListCg);

          // 更新数据
          that.setData({
            purchaseListSh: combinedList
          });

          console.log("zzzzzzzzzzzzzzzzzzzzzzz",purchaseListSh);

        } else {
          console.log("配货未完成的无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },


  // 获取拣货单已完成数据
  getPurchaseOrderList(searchTerm) {
    console.log("进入了");
    var that = this;

    // 在请求开始前先清空数组
    that.setData({
      purchaseList: []  // 清空数组
    });

    var currentContentId = that.data.currentContentId
    console.log("ddddddddddd",);
    var bx_status = 0;
    if(currentContentId == 2){
      bx_status = 1
    }

    var shujuKu = app.globaldata.cipher_code
    console.log("数据库==>",shujuKu);

    var data = JSON.stringify({
      "q": {
        "encrypted_text": "SgwUb82m53BFzwTxBjrxZw==OXy8xgxbXx+znYcy5MNTNoaWz2NuhuMe1Iz8983nkbo=",
        "key": "65af0c184f878a00",
        "sql": "select  T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t4.FNAME as 客户名称 ,SUM(T2.F_ZYKJ_QTY12) AS 拣货数量,sum(t2.fseq) as 总款数 from ZYKJ_SCRWD T1 inner join ZYKJ_SCRWD_Entry T2 ON T2.FID=T1.FID INNER JOIN F_SCRWD_Entry1 T3 ON T3.FID=T1.FID LEFT JOIN T_BD_CUSTOMER_L T4 ON T4.FCUSTID =T1.F_ZYKJ_Base_apv WHERE T1.FBillTypeId ='662f0114a2b069' and T1.FDOCUMENTSTATUS='C' AND T2.F_ZYKJ_BILLSTATUS_83G='1' and t1.FBILLNO LIKE '%" + searchTerm + "%' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t4.FNAME ,t1.F_ZYKJ_Base_tzk1 UNION ALL select  T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t4.FNAME as 客户名称,SUM(T3.F_ZYKJ_Qty_83g) AS 拣货数量,sum(t3.Fseqs) as 总款数 from ZYKJ_SCRWD T1 inner join ZYKJ_SCRWD_Entry T2 ON T2.FID=T1.FID INNER JOIN F_SCRWD_Entry1 T3 ON T3.FID=T1.FID LEFT JOIN T_BD_CUSTOMER_L T4 ON T4.FCUSTID =T1.F_ZYKJ_Base_apv WHERE T1.FBillTypeId ='662f0125a2b0f7'and T1.FDOCUMENTSTATUS='C' AND T3.F_ZYKJ_BILLSTATUS_RE5='1' and t1.FBILLNO LIKE '%" + searchTerm + "%' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t4.FNAME ,t1.F_ZYKJ_Base_tzk1 UNION ALL select  T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t4.FNAME as 客户名称,SUM(T2.F_ZYKJ_QTY12) AS 拣货数量,sum(t2.fseq) as 总款数 from ZYKJ_SCRWD T1 inner join ZYKJ_SCRWD_Entry T2 ON T2.FID=T1.FID INNER JOIN F_SCRWD_Entry1 T3 ON T3.FID=T1.FID LEFT JOIN T_BD_CUSTOMER_L T4 ON T4.FCUSTID =T1.F_ZYKJ_Base_apv WHERE T1.FBillTypeId ='6628a101c794d4' and T1.FDOCUMENTSTATUS='C' AND T2.F_ZYKJ_BILLSTATUS_83G='1' and t2.F_ZYKJ_CHECKBOX1 ='0' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t4.FNAME ,t1.F_ZYKJ_Base_tzk1 UNION ALL select  T1.FID AS FID,T1.F_ZYKJ_Text AS 订单号,T1.FBILLNO AS 单据编码,t1.F_ZYKJ_DATE  as 业务日期,T1.F_ZYKJ_Base_apv AS 客户ID,t4.FNAME as 客户名称,SUM(T3.F_ZYKJ_Qty_83g) AS 拣货数量,sum(t3.Fseqs) as 总款数 from ZYKJ_SCRWD T1 inner join ZYKJ_SCRWD_Entry T2 ON T2.FID=T1.FID INNER JOIN F_SCRWD_Entry1 T3 ON T3.FID=T1.FID LEFT JOIN T_BD_CUSTOMER_L T4 ON T4.FCUSTID =T1.F_ZYKJ_Base_apv WHERE T1.FBillTypeId ='6628a101c794d4'and T1.FDOCUMENTSTATUS='C' AND T3.F_ZYKJ_BILLSTATUS_RE5='1'and t2.F_ZYKJ_CHECKBOX1 ='1' and t1.FBILLNO LIKE '%" + searchTerm + "%' GROUP BY T1.FID,T1.F_ZYKJ_Text,T1.FBILLNO,t1.F_ZYKJ_DATE ,T1.F_ZYKJ_Base_apv ,t4.FNAME ,t1.F_ZYKJ_Base_tzk1 order by t1.FID",
        "fields": [
          "FID","订单号","单据编码","业务日期","客户ID","客户名称","拣货数量","总款数"
        ]
      },
      "p": {
        "page_number": 1,
        "page_size": 20
      }
      })

    dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        console.log('获取配货单未完成数据响应==>', res.data)
        if (res.data.code == 200) {
        console.log('获取配货单未完成数据响应200000==>', res.data)
          var list = res.data.data;
          var purchaseOrderList = that.data.purchaseOrderList;
          for(var i in list){
            purchaseOrderList.push(list[i])
          }
          if(list.length < that.data.page_size){
            that.setData({
              dowm:false
            })
          }
          that.setData({
            purchaseOrderList:purchaseOrderList
          })
          console.log("数据",purchaseOrderList);
        } else {
          console.log("配货未完成的无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },

  getIndex(e) {
    console.log("type==>e", e)
  },

  onBlur(e) {
    // var that = this
    console.log('onBlur', e.detail)
    // var item = that.data.item
    // item.基本单位数量 =  e.detail
    // this.setData({
    //   item,
    // });
  },


// 未完成点击开始拣货散货
  gopickingListTwo(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    item.currentContentId = this.data.currentContentId; // 添加当前状态到 item 对象中
    dd.navigateTo({
      url: '/pages/purchaseTwo/purchaseTwo?item='+JSON.stringify(item),
    })
  },

  // 未完成点击开始拣货成品
  gopickingListTwor(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    item.currentContentId = this.data.currentContentId; // 添加当前状态到 item 对象中
    dd.navigateTo({
      url: '/pages/purchaseTwor/purchaseTwor?item='+JSON.stringify(item),
    })
  },



// 已完成点击查看详情
  gopickingListTwoy(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/purchaseTwosy/purchaseTwosy?item='+JSON.stringify(item),

    })
  },


  onReachBottom(){
    var that = this;
    var page = that.data.page;
    var dowm = that.data.dowm;
    var currentContentId = that.data.currentContentId;
    if(dowm){
      that.setData({
        page:page+1
      })
  
      if(currentContentId < 2 ){
        this.getPurchaseOrderList();
      }else{
        // this.getReimbursementList();
      }
      
    }
    
  },


});









